package com.juyu.miliao.scheduler.dao.redis;

import io.lettuce.core.KeyScanCursor;
import io.lettuce.core.RedisClient;
import io.lettuce.core.ScanArgs;
import io.lettuce.core.ScanCursor;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.api.sync.RedisStringCommands;
import io.lettuce.core.codec.Utf8StringCodec;
import io.lettuce.core.protocol.CommandArgs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisClusterNode;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.StreamSupport;

/**
 * @ClassName: BeanWelfareRedisDao
 * @Description: TODO
 * @Author zhangcheng
 * @Date 19-9-18上午9:10
 */
@Repository
public class BeanWelfareRedisDao {
    private final Logger beanWelfareLogger = LoggerFactory.getLogger("beanWelfare");
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 重置寻豆福利奖励领取状态
     */
    public void resetBeanWelfareStatues() {
        RedisClusterConnection redisClusterConnection = stringRedisTemplate.getConnectionFactory().getClusterConnection();
        StreamSupport.stream(redisClusterConnection.clusterGetNodes().spliterator(), false)
                .filter(RedisClusterNode::isMaster)
                .forEach(redisClusterNode -> {
                    RedisClient client = RedisClient.create("redis://" + redisClusterNode.getHost() + ":" + redisClusterNode.getPort());
                    try (StatefulRedisConnection<String, String> connection = client.connect();) {
                        RedisCommands<String, String> commands = connection.sync();
                        ScanArgs scanArgs = ScanArgs.Builder.matches("bean_welfare:*");
                        ScanCursor cursor = ScanCursor.INITIAL;
                        while (true) {
                            KeyScanCursor<String> next = commands.scan(cursor, scanArgs);
                            List<String> keys = next.getKeys();
                            if (keys != null && !keys.isEmpty()) {
                                commands.del(keys.toArray(new String[0]));
                                beanWelfareLogger.info(redisClusterNode.getHost() + ":" + redisClusterNode.getPort() + " 删除" + keys.size() + "个key:" + keys);
                            }
                            if (next.isFinished()) {
                                break;
                            } else {
                                cursor = new ScanCursor(next.getCursor(), false);
                            }
                        }
                    } finally {
                        client.shutdown();
                    }
                });
    }

}
